Array.prototype.insert = function (n) {
  if (isNaN(n)) return this
  if (this[this.length - 1] < this[0]) this.reverse()
  if (n >= this[this.length - 1] || this.length === 0) {
    this.push(n)
  } else {
    for (var i = 0; i < this.length; i++) {
      if (n <= this[i]) {
        this.splice(i, 0, n)
        break
      }
    }
  }
  if (this[this.length - 1] < this[0]) this.reverse()
  return this
}
//键值对转换为对象
function parseQueryString(str) {
  var obj = {}
  str.split('&').forEach(function (item) {
    var tmp = item.split('=')
    obj[ tmp[0] ] = tmp[1]
  })
  return obj
}
//拼接键值对
function queryStringify(obj) {
  var str = ''
  for (var k in obj) str += `${ k }=${ obj[k] }&`
  return str.slice(0, -1)
}
 //生成随机数（0～255）
function randomNum(a = 255, b = 0) {
  return Math.floor(Math.random() * (Math.abs(a - b) + 1)) + Math.min(a, b)
}

function randomColor() {
  return `rgb(${ randomNum() }, ${ randomNum() }, ${ randomNum() })`
}

function diffTime(start, end) {
  var sub = Math.ceil((end - start) / 1000)
  return {
    day: Math.floor(sub / (60 * 60 * 24)),
    hours: Math.floor(sub % (60 * 60 * 24) / (60 * 60)),
    minutes: Math.floor(sub % (60 * 60) / 60),
    seconds: sub % 60
  }
}
